package com.itheima.controller;

import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请求处理类
 */
@Slf4j  // 日志
@RestController
@RequestMapping("/depts")
public class DeptController {
    /**
     * 部门列表查询
     * @return
     */
    /*@RequestMapping("/depts")
    public List<Dept> getAll(){
        //1.加载.txt文件
        InputStream input = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
        List<String> strings = IOUtils.readLines(input, "utf-8");

        //2.解析文本数据，封装成集合
        //遍历集合中的每行数据
        List<Dept> depts = strings.stream().map((str)->{
            //分割每行数据
            String[] parts = str.split(",");
            Integer id = Integer.valueOf(parts[0]);
            String name = parts[1];
            LocalDateTime updateTime = LocalDateTime.parse(parts[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new Dept(id, name, updateTime);
        }).toList();

        //3.返回数据（json）
        return depts;
    }*/

    // 改造
    // @RequestMapping(value = "/depts",method = RequestMethod.GET)

    /*@GetMapping("/depts")
    public Result getAll(){
        //1.加载.txt文件
        InputStream input = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
        List<String> strings = IOUtils.readLines(input, "utf-8");

        //2.解析文本数据，封装成集合
        //遍历集合中的每行数据
        List<Dept> depts = strings.stream().map((str)->{
            //分割每行数据
            String[] parts = str.split(",");
            Integer id = Integer.valueOf(parts[0]);
            String name = parts[1];
            LocalDateTime updateTime = LocalDateTime.parse(parts[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new Dept(id, name, updateTime);
        }).toList();

        //3.返回数据（json）
        return Result.success(depts);
    }*/

    /*private DeptService deptService = new DeptServiceImpl();*/
    @Autowired //从IOC容器中获取对象  --依赖注入DI的实现
    private DeptService deptService;

    @GetMapping
    public Result List(){
        //1.调用Service层
        List<Dept> depts = deptService.list();
        //3.返回数据（json）
        return Result.success(depts);
    }

    //原始删除方法
    /*@DeleteMapping("/depts")
    public Result delete(HttpServletRequest  request){
        //1.接收参数
        String id = request.getParameter("id");
        int idInt = Integer.parseInt(id);
        System.out.println("idInt = " + idInt);
        //2.调用Service层

        //3.返回数据（json）
        return Result.success();
    }*/

    //方式2：通过spring提供的@RequestParam注解
    @DeleteMapping
    //public Result delete(@RequestParam ("id") Integer deptId){
    public Result delete(Integer id){
        //1.接收参数
        //log.info("deptId = " + id);
        //log.info("id = {}",id);
        System.out.println("id = " + id);
        //2.调用Service层
        deptService.delete(id);
        //3.返回数据（json）
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Dept dept){
        //log.info("dept = {}", dept);
        System.out.println("dept = " + dept);
        //调用Service层
        deptService.sava(dept);
        return Result.success();
    }

    /**
     * 回显部门数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        //log.info("id = {}", id);
        System.out.println("id = " + id);
        //调用Service层
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        //log.info("dept = {}", dept);
        System.out.println("dept = " + dept);
        deptService.update(dept);
        return Result.success();
    }


}
